<?php 
	
	class User_model extends CI_Model {
		
		var $tblName = "admin_users";
		 
		function __construct()
		{
			parent::__construct();
		}
		
		function save_user($record, $id)
		{	
			$id = (int)$id;
			if( $id > 0 ) 
			{
				$this->db->where('id', $id);
				$this->db->update($this->tblName, $record);
				DB_modification_logs("user");
				return $id;
			}
			else
			{
				$this->db->insert($this->tblName, $record);	
				$id = $this->db->insert_id();
				DB_modification_logs("user");
				return $id;
			}
		} // end save_user
		
		function save_permission($POST, $user_id)
		{
			
			$sql_delete = " DELETE from modules_permission where user_id = ".$user_id;		
			$this->db->query($sql_delete);			
			
			$modules_arr = $POST['modules'];
			$permission_arr = $POST['permissions'];
			if( count($modules_arr) > 0 )  // any module selected
			{
				foreach( $modules_arr as $module_id) 
				{
					//echo $module."-";
					$module_permissions = $permission_arr[$module_id];
					
					$user_module_per_arr = array();
					
					if ( $module_permissions > 0 ) 
					{
						foreach( $module_permissions as $module_per) 
						{
							$user_module_per_arr[] = $module_per;
						}	
					}
					
					$str_permission = implode(',', $user_module_per_arr);
					
					$record = array(
									'module_id' => $module_id,
									'permission' => addslashes($str_permission),
									'user_id' => $user_id);
					
					$this->db->insert("modules_permission", $record);
				}
			}
		}
		
				
		function getDetailByID($id)
		{
		  $id = (int)$id;	
		  $sql = "SELECT * FROM " . $this->tblName . " WHERE id = ".$id;
		  return $this->db->query($sql)->row_array();
		}
		
		function getListingData($GET, $start, $offset = 20)
		{
 			$where = " WHERE ( user_type <> 'admin' OR user_type is NULL ) ";
			if( isset($GET['username']) && trim($GET['username']) != '' )
			{
				$where  .= " AND username LIKE '%".trim($this->db->escape($GET['username']),"'")."%' ";	
			}
			if( isset($GET['full_name']) && trim($GET['full_name']) != '' )
			{
				$where  .= " AND full_name LIKE '%".trim($this->db->escape($GET['full_name']),"'")."%' ";	
			}
			
			$LIMIT = "LIMIT $start ,  $offset ";
			$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . $this->tblName . " {$where} order by username {$LIMIT} ";
			$results = $this->db->query($sql)->result();	
			$total_record = $this->common_model->get_found_rows();
			
			return array('results' => $results, 'total_record' => $total_record);
		}
		
		function getUserModules($user_id)
		{
			$user_id = (int)$user_id;
			$sql = "select  m.id, m.name, m.url, mp.permission 
					FROM modules m INNER JOIN modules_permission mp ON m.id = mp.module_id
					WHERE m.active = 1 AND mp.user_id = ".$user_id." ORDER BY ordering";
			return $this->db->query($sql)->result();		
		}
		
		function getModules()
		{
			$sql = "select * from modules where active = 1 and url <> 'user' order by ordering ASC";
			return $this->db->query($sql)->result();
		}
		
	
  }
?>